<?php
class DataPlayer{
    public $PlayerNameBytes;
    public $MailAddressBytes;
    public $PasswordHashBytes;
    public $SecretKeyBytes;
    public $level;
    public $money;
    public $chip;
    public $exp;
    public $GemOfPlayer = array();
    public $PowOfPlayer = array();
    function __construct() {
        $this->level = 0;
        $this->money = 1000;
        $this->chip =200;
        $this->exp = 0;
        for($i = 2 ; $i < 6; $i++)
        {
            $this->GemOfPlayer[$i] = 0;
        }
        for($i = 0 ; $i < 9; $i++)
        {
            $this->PowOfPlayer[$i] = 200;
        }
    }
    static function swap($bytebuffer) {
        $player = new DataPlayer();
        $player->MailAddressBytes = DataUtil::readBBin( $bytebuffer ) ;
        $player->PasswordHashBytes = DataUtil::readBBin( $bytebuffer ) ;
        $player->SecretKeyBytes = DataUtil::readBBin( $bytebuffer ) ;
        $player->PlayerNameBytes = DataUtil::readBBin($bytebuffer) ;
        $player->level = DataUtil::readInt32($bytebuffer);
        $player->money = DataUtil::readInt32($bytebuffer);
        $player->chip = DataUtil::readInt32($bytebuffer);
        $player->exp = DataUtil::readInt32($bytebuffer);
        $num_gem_type = DataUtil::readInt32($bytebuffer);
        for ($i = 2; $i < $num_gem_type + 2; $i++) {
            $player->GemOfPlayer[$i] = DataUtil::readInt32($bytebuffer);
        }
        $num_pow_type = DataUtil::readInt32($bytebuffer);
        for ($i = 0; $i < $num_pow_type; $i++) {
            $player->PowOfPlayer[$i] = DataUtil::readInt32($bytebuffer);
        }
        return $player;
	}
    function setName($name) {
       $this->PlayerNameBytes = unpack('C*', $name ) ;
    }
    function getName()
    {
        return implode(array_map("chr", $this->PlayerNameBytes));
    }
    function  setMailAddress($mailaddress)
    {
        $this->MailAddressBytes = unpack('C*', $mailaddress);
    }
    function getMailAddress()
    {
         return implode(array_map("chr", $this->MailAddressBytes));
    }
    function getPasswordHash() {
        return implode(array_map("chr", $this->PasswordHashBytes));
    }
    function setPasswordHash($hash) {
        $this->PasswordHashBytes = unpack('C*', $hash);
    }
    function getSecretKey() {
        return implode(array_map("chr", $this->SecretKeyBytes));
    }
    function getBinSize() {
       $size = 0;
       $size += 1  + count($this->MailAddressBytes);
       $size += 1  + count($this->PasswordHashBytes);
       $size += 1  + count($this->SecretKeyBytes);
       $size += 1  + count($this->PlayerNameBytes);
       $size += 4;
       $size += 4;
       $size += 4;
       $size += 4;
       $size += 4;
       $size += 4 * count($this->GemOfPlayer);
       $size += 4;
       $size += 4 * count($this->PowOfPlayer);
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeBBin( $buf ,  $this->MailAddressBytes ) ;
		DataUtil::writeBBin( $buf ,  $this->PasswordHashBytes ) ;
		DataUtil::writeBBin( $buf ,  $this->SecretKeyBytes ) ;
                DataUtil::writeBBin($buf  ,  $this->PlayerNameBytes);            
		DataUtil::writeInt32($buf, $this->level);
                DataUtil::writeInt32($buf, $this->money);
                DataUtil::writeInt32($buf, $this->chip);
                DataUtil::writeInt32($buf, $this->exp);
                DataUtil::writeInt32( $buf ,  count($this->GemOfPlayer) ) ;
		if(is_array($this->GemOfPlayer)){
			foreach($this->GemOfPlayer as $item){
				DataUtil::writeInt32( $buf ,  $item ) ;
			}
		}
                DataUtil::writeInt32( $buf ,  count($this->PowOfPlayer) ) ;
                if(is_array($this->PowOfPlayer)){
			foreach($this->PowOfPlayer as $item){
				DataUtil::writeInt32( $buf ,  $item ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>